package com.ptbuy.db.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ptbuy.db.pojo.ClientPojo;
import com.ptbuy.db.pojo.ProductPojo;
import com.ptbuy.statanalysys.DataValuePojo;
import com.ptbuy.utils.Utils;
import com.ptbuy.base.BaseApplication;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class ProductDao {
    public SQLiteDatabase sqLiteOpenHelper;

    public ProductDao() {
        sqLiteOpenHelper = BaseApplication.getDb();
    }


    //删除商品信息
    public synchronized int delInfo(String id) {
        try {
            int app_phase_config = sqLiteOpenHelper.delete("product", "id=?", new String[]{id});
            return app_phase_config;
        } catch (Exception e) {
            return -1;
        }
    }

    //通过客户id删除商品信息,删除客户信息的时候一同删除商品信息
    public synchronized int delInfoByPid(String pId) {
        try {
            int app_phase_config = sqLiteOpenHelper.delete("product", "pid=?", new String[]{pId});
            return app_phase_config;
        } catch (Exception e) {
            return -1;
        }
    }

    //清空列表的时候
    public synchronized int delInfoByStatus() {
        try {
            int app_phase_config = sqLiteOpenHelper.delete("product", "status=?", new String[]{"0"});
            return app_phase_config;
        } catch (Exception e) {
            return -1;
        }
    }


    /**
     * 新增商品信息
     *
     * @return
     */
    public synchronized int addInfo(String productName, String productPrice, String mult, String primitiveProductPrice, String currentTime,Integer mulPic,Integer mulVideo) {
        sqLiteOpenHelper.beginTransaction();
        try {
            ContentValues values = new ContentValues();
            values.put("id", UUID.randomUUID().toString().replace("-", ""));
            values.put("product_name", productName);
            values.put("product_price", productPrice);
            values.put("primitive_product_price", primitiveProductPrice);
            values.put("mult", mult);
            values.put("time", currentTime);
            values.put("status", 0);
            values.put("mult_pic",mulPic);
            values.put("mult_video", mulVideo);
            long insertCount = sqLiteOpenHelper.insert("product", null, values);
            sqLiteOpenHelper.setTransactionSuccessful();
        } finally {
            sqLiteOpenHelper.endTransaction();
        }
        return 1;
    }

    /**
     * 修改
     */
    public synchronized Integer updateStatusInfo(String pid) {
        try {
            ContentValues values = new ContentValues();
            values.put("status", 1);
            values.put("pid", pid);
            String[] args = {"0"};
            int update = sqLiteOpenHelper.update("product", values, "status=?", args);
            return update;
        } catch (Exception e) {
            return -1;
        }
    }

    public synchronized Integer updateInfo(String id, String productName, String productPrice) {
        try {
            ContentValues values = new ContentValues();
            values.put("product_name", productName);
            values.put("product_price", productPrice);
            String[] args = {id};
            int update = sqLiteOpenHelper.update("product", values, "id=?", args);
            return update;
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 修改照片
     *
     * @param id
     * @param mult
     * @return
     */
    public synchronized Integer updateInfoMult(String id, String mult,Integer mulPic,Integer mulVideo) {
        try {
            ContentValues values = new ContentValues();
            values.put("mult", mult);
            values.put("mult_pic",mulPic);
            values.put("mult_video", mulVideo);
            String[] args = {id};
            int update = sqLiteOpenHelper.update("product", values, "id=?", args);
            return update;
        } catch (Exception e) {
            return -1;
        }
    }

    //查询商品信息，默认查询状态为0的,或者通过客户表id查询商品
    @SuppressLint("Range")
    public synchronized List<ProductPojo> queryExportPojo(String pid) {
        List<ProductPojo> list = new ArrayList<>();
        try {
            String sql = "select id,product_name,product_price,mult,pid,time,status,primitive_product_price,myorder,mult_pic,mult_video  from product where status = 0 order by myorder desc";
            Cursor cursor = sqLiteOpenHelper.rawQuery(sql, null);
            if (pid != null) {
                sql = "select id,product_name,product_price,mult,pid,time,status,primitive_product_price,myorder,mult_pic,mult_video  from product where pid = ?  order by myorder desc";
                cursor = sqLiteOpenHelper.rawQuery(sql, new String[]{pid});
            }


            ProductPojo recodInfo = null;
            if (cursor.moveToFirst()) {
                do {
                    recodInfo = new ProductPojo();
                    recodInfo.setId(cursor.getString(cursor.getColumnIndex("id")));
                    recodInfo.setProductName(cursor.getString(cursor.getColumnIndex("product_name")));
                    recodInfo.setProductPrice(cursor.getString(cursor.getColumnIndex("product_price")));
                    recodInfo.setPrimitiveProductPrice(cursor.getString(cursor.getColumnIndex("primitive_product_price")));
                    recodInfo.setMult(cursor.getString(cursor.getColumnIndex("mult")));
                    recodInfo.setPid(cursor.getString(cursor.getColumnIndex("pid")));
                    recodInfo.setTime(cursor.getString(cursor.getColumnIndex("time")));
                    recodInfo.setStatus(cursor.getInt(cursor.getColumnIndex("status")));
                    recodInfo.setMyorder(cursor.getInt(cursor.getColumnIndex("myorder")));
                    recodInfo.setMultPic(cursor.getInt(cursor.getColumnIndex("mult_pic")));
                    recodInfo.setMultVideo(cursor.getInt(cursor.getColumnIndex("mult_video")));
                    list.add(recodInfo);
                } while (cursor.moveToNext());
            }
            cursor.close();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return list;
    }


    @SuppressLint("Range")
    public synchronized ProductPojo queryProductPojoById(String id) {
        ProductPojo recodInfo = null;
        try {
            String sql = "select id,product_name,product_price,mult,pid,time,status,primitive_product_price,myorder,mult_pic,mult_video  from product where id = ?";
            Cursor cursor = sqLiteOpenHelper.rawQuery(sql, new String[]{id});
            if (cursor.moveToFirst()) {
                do {
                    recodInfo = new ProductPojo();
                    recodInfo.setId(cursor.getString(cursor.getColumnIndex("id")));
                    recodInfo.setProductName(cursor.getString(cursor.getColumnIndex("product_name")));
                    recodInfo.setProductPrice(cursor.getString(cursor.getColumnIndex("product_price")));
                    recodInfo.setPrimitiveProductPrice(cursor.getString(cursor.getColumnIndex("primitive_product_price")));
                    recodInfo.setMult(cursor.getString(cursor.getColumnIndex("mult")));
                    recodInfo.setPid(cursor.getString(cursor.getColumnIndex("pid")));
                    recodInfo.setTime(cursor.getString(cursor.getColumnIndex("time")));
                    recodInfo.setStatus(cursor.getInt(cursor.getColumnIndex("status")));
                    recodInfo.setMyorder(cursor.getInt(cursor.getColumnIndex("myorder")));
                    recodInfo.setMultPic(cursor.getInt(cursor.getColumnIndex("mult_pic")));
                    recodInfo.setMultVideo(cursor.getInt(cursor.getColumnIndex("mult_video")));
                } while (cursor.moveToNext());
            }
            cursor.close();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return recodInfo;
    }


    /**
     * 查询所有信息，状态不为0的，状态为0的时候是临时保存的信息
     *
     * @return
     */
    @SuppressLint("Range")
    public synchronized List<ProductPojo> queryInfo(int offset, int pageSize, String sqlParam) {
        List<ProductPojo> list = new ArrayList<>();
        try {
            String sql = "select a.id aid,a.product_name aproduct_name,a.product_price aproduct_price,a.primitive_product_price primitive_product_price," +
                    "a.mult amult,a.pid apid,a.time atime,a.status astatus,mult_pic,mult_video," +
                    "b.id bid,b.agent_name bagentName,b.name bname,b.phone bphone,b.time btime,b.total_price btotal_price,b.total_product btotal_product" +
                    "  from product a LEFT JOIN client b on a.pid=b.id" +
                    " where a.status != 0";

            if (sqlParam != null && !"".equalsIgnoreCase(sqlParam)) {
                sql += sqlParam;
            } else {
                sql += " order by a.myorder desc ";
            }
            if (pageSize > 0) {
                sql += " order by a.myorder desc LIMIT " + pageSize + " OFFSET " + offset;
            } else {
                sql = sql.replace("desc", "asc");
            }

            Cursor cursor = sqLiteOpenHelper.rawQuery(sql, null);
            ProductPojo recodInfo = null;
            ClientPojo clientPojo = null;
            if (cursor.moveToFirst()) {
                do {
                    recodInfo = new ProductPojo();
                    recodInfo.setId(cursor.getString(cursor.getColumnIndex("aid")));
                    recodInfo.setProductName(cursor.getString(cursor.getColumnIndex("aproduct_name")));
                    recodInfo.setProductPrice(cursor.getString(cursor.getColumnIndex("aproduct_price")));
                    recodInfo.setPrimitiveProductPrice(cursor.getString(cursor.getColumnIndex("primitive_product_price")));
                    recodInfo.setMult(cursor.getString(cursor.getColumnIndex("amult")));
                    recodInfo.setPid(cursor.getString(cursor.getColumnIndex("apid")));
                    recodInfo.setTime(cursor.getString(cursor.getColumnIndex("atime")));
                    recodInfo.setStatus(cursor.getInt(cursor.getColumnIndex("astatus")));
                    recodInfo.setMultPic(cursor.getInt(cursor.getColumnIndex("mult_pic")));
                    recodInfo.setMultVideo(cursor.getInt(cursor.getColumnIndex("mult_video")));

                    clientPojo = new ClientPojo();
                    clientPojo.setId(cursor.getString(cursor.getColumnIndex("bid")));
                    clientPojo.setName(cursor.getString(cursor.getColumnIndex("bname")));
                    clientPojo.setAgentName(cursor.getString(cursor.getColumnIndex("bagentName")));
                    clientPojo.setTotalProduct(cursor.getInt(cursor.getColumnIndex("btotal_product")));
                    clientPojo.setTotalPrice(cursor.getString(cursor.getColumnIndex("btotal_price")));
                    clientPojo.setTime(cursor.getString(cursor.getColumnIndex("btime")));
                    clientPojo.setPhone(cursor.getString(cursor.getColumnIndex("bphone")));
                    recodInfo.setClientPojo(clientPojo);

                    list.add(recodInfo);
                } while (cursor.moveToNext());
            }
            cursor.close();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return list;
    }


    /**
     * 查询前10个商品名称
     *
     * @param productName
     * @return
     */
    @SuppressLint("Range")
    public synchronized List<String> queryProductName(String productName) {
        List<String> list = new ArrayList<>();
        try {
            String sql = "select DISTINCT product_name  from product where product_name like ?" + " LIMIT 10 OFFSET 0";
            Cursor cursor = sqLiteOpenHelper.rawQuery(sql, new String[]{productName});
            if (cursor.moveToFirst()) {
                do {
                    list.add(cursor.getString(cursor.getColumnIndex("product_name")));
                } while (cursor.moveToNext());
            }
            cursor.close();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return list;
    }


    //查询商品信息，默认查询状态为0的,或者通过客户表id查询商品
    @SuppressLint("Range")
    public synchronized List<DataValuePojo> queryProductAndPriceo() {
        List<DataValuePojo> list = new ArrayList<>();
        try {
            String sql = "SELECT product_name, product_price FROM product where status != 0 GROUP BY product_name ";
            Cursor cursor = sqLiteOpenHelper.rawQuery(sql, null);

            DataValuePojo recodInfo = null;
            if (cursor.moveToFirst()) {
                do {
                    recodInfo = new DataValuePojo();
                    recodInfo.setName(cursor.getString(cursor.getColumnIndex("product_name")));
                    recodInfo.setValue(cursor.getString(cursor.getColumnIndex("product_price")));
                    list.add(recodInfo);
                } while (cursor.moveToNext());
            }
            cursor.close();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return list;
    }
}
